package com.gaofans.nrpc.common.context;

import com.gaofans.nrpc.common.processor.Builder;
import com.gaofans.nrpc.common.protocol.RpcResponse;
import io.netty.util.concurrent.EventExecutor;

import java.io.Serializable;
import java.util.Map;
import java.util.Objects;

/**
 * @author GaoFans
 */
public class ContextResponse extends RpcResponse implements Context{

    private EventExecutor executor;
    private final RpcResponse response;

    private ContextResponse(RpcResponse response) {
        this.response = response;
    }

    public static class ContextResponseBuilder implements Builder<ContextResponse>{

        private ContextResponse contextResponse;
        public ContextResponseBuilder(RpcResponse response) {
            this.contextResponse = new ContextResponse(Objects.requireNonNull(response));
        }

        public ContextResponseBuilder executor(EventExecutor executor){
            contextResponse.executor = executor;
            return this;
        }

        public ContextResponseBuilder executor(SelectableExecutor chooser){
            contextResponse.executor = chooser.select(contextResponse.response.getSequenceId());
            return this;
        }

        @Override
        public ContextResponse build() {
            Objects.requireNonNull(contextResponse.executor);
            return contextResponse;
        }
    }

    @Override
    public EventExecutor executor() {
        return executor;
    }

    public RpcResponse getResponse() {
        return response;
    }

    @Override
    public long getSequenceId() {
        return response.getSequenceId();
    }

    @Override
    public void setSequenceId(long sequenceId) {
        response.setSequenceId(sequenceId);
    }

    @Override
    public void putHeader(String key, Serializable value) {
        response.putHeader(key, value);
    }

    @Override
    public Serializable header(String key) {
        return response.header(key);
    }

    @Override
    public Serializable removeHeader(String key) {
        return response.removeHeader(key);
    }

    @Override
    public Map<String, Serializable> headers() {
        return response.headers();
    }

    @Override
    public String getError() {
        return response.getError();
    }

    @Override
    public void setError(String error) {
        response.setError(error);
    }

    @Override
    public Object getResult() {
        return response.getResult();
    }

    @Override
    public void setResult(Object result) {
        response.setResult(result);
    }

    @Override
    public boolean isVoid() {
        return response.isVoid();
    }

    @Override
    public void setVoid(boolean aVoid) {
        response.setVoid(aVoid);
    }

    @Override
    public boolean isSuccess() {
        return response.isSuccess();
    }

    @Override
    public void setSuccess(boolean success) {
        response.setSuccess(success);
    }

    @Override
    public byte getType() {
        return response.getType();
    }

    @Override
    public boolean isStream() {
        return response.isStream();
    }

    @Override
    public void setStream(boolean stream) {
        response.setStream(stream);
    }

    @Override
    public boolean isEnd() {
        return response.isEnd();
    }

    @Override
    public void setEnd(boolean end) {
        response.setEnd(end);
    }
}
